//
//  ZJHAccount.m
//  ZiJinHuaDemo
//
//  Created by 李宗帅 on 2018/4/2.
//  Copyright © 2018年 ZiJinHuaLive.com. All rights reserved.
//

#import "ZJHAccount.h"

@implementation ZJHAccount

#define ZQAccountFilePath  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"]

static ZJHAccount *account;


+(instancetype)accountWithDic:(NSDictionary *)dic
{
    ZJHAccount * account = [[self alloc]init];
    account.userID =[NSString stringWithFormat:@"%@",dic[@"userId"]];
    account.userAccount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAccount"]];
    account.token = [NSString stringWithFormat:@"%@",[dic valueForKey:@"token"]];
    account.nickName = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickname"]];
    account.imgPath = [NSString stringWithFormat:@"%@",[dic valueForKey:@"imgPath"]];
    account.mobile = [NSString stringWithFormat:@"%@",[dic valueForKey:@"mobile"]];
    account.sex = [NSString stringWithFormat:@"%@",[dic valueForKey:@"sex"]];
    account.createTime = [NSString stringWithFormat:@"%@",[dic valueForKey:@"createTime"]];
    account.email = [NSString stringWithFormat:@"%@",[dic valueForKey:@"email"]];
    account.trueName = [NSString stringWithFormat:@"%@",[dic valueForKey:@"truename"]];
    account.className = [NSString stringWithFormat:@"%@",[dic valueForKey:@"className"]];
    account.schoolName = [NSString stringWithFormat:@"%@",[dic valueForKey:@"schoolName"]];
    account.gradeName = [NSString stringWithFormat:@"%@",[dic valueForKey:@"gradeName"]];
    //    account.longitude = [NSString stringWithFormat:@"%@",[dic valueForKey:@"longitude"]];
    //    account.latitude = [NSString stringWithFormat:@"%@",[dic valueForKey:@"latitude"]];
    
    return account;
}

/**
 *  当从文件中解析出一个对象的时候调用
 *  在这个方法中写清楚：怎么解析文件中的数据
 */
-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        self.userID =[aDecoder decodeObjectForKey:@"userId"];
        self.nickName = [aDecoder decodeObjectForKey:@"nickname"];
        self.token = [aDecoder decodeObjectForKey:@"token"];
        self.imgPath = [aDecoder decodeObjectForKey:@"imgPath"];
        self.userAccount = [aDecoder decodeObjectForKey:@"userAccount"];
        self.mobile = [aDecoder decodeObjectForKey:@"mobile"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
        self.token = [aDecoder decodeObjectForKey:@"token"];
        self.createTime = [aDecoder decodeObjectForKey:@"createTime"];
        self.email = [aDecoder decodeObjectForKey:@"email"];
        self.trueName = [aDecoder decodeObjectForKey:@"truename"];
        self.className = [aDecoder decodeObjectForKey:@"className"];
        self.schoolName = [aDecoder decodeObjectForKey:@"schoolName"];
        self.gradeName = [aDecoder decodeObjectForKey:@"gradeName"];
        //        self.longitude = [aDecoder decodeObjectForKey:@"longitude"];
        //        self.latitude = [aDecoder decodeObjectForKey:@"latitude"];
    }
    
    return self;
}
/**
 *  将对象写入文件的时候调用
 *  在这个方法中写清楚：要存储哪些对象的哪些属性，以及怎样存储属性
 */
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.userID forKey:@"userId"];
    [aCoder encodeObject:self.nickName forKey:@"nickname"];
    [aCoder encodeObject:self.imgPath forKey:@"imgPath"];
    [aCoder encodeObject:self.userAccount forKey:@"userAccount"];
    [aCoder encodeObject:self.mobile forKey:@"mobile"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    [aCoder encodeObject:self.trueName forKey:@"truename"];
    [aCoder encodeObject:self.email forKey:@"email"];
    [aCoder encodeObject:self.token forKey:@"token"];
    [aCoder encodeObject:self.createTime forKey:@"createTime"];
    [aCoder encodeObject:self.schoolName forKey:@"schoolName"];
    [aCoder encodeObject:self.className forKey:@"className"];
    [aCoder encodeObject:self.gradeName forKey:@"gradeName"];
    //    [aCoder encodeObject:self.longitude forKey:@"longitude"];
    //    [aCoder encodeObject:self.latitude forKey:@"latitude"];
}

/// 账号归档
+ (void)save:(ZJHAccount *)newAccount {
    account=newAccount;
    
    [NSKeyedArchiver archiveRootObject:newAccount toFile:ZQAccountFilePath];
}

/// 账号反归档
+ (ZJHAccount *)account {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //读取账号 解挡
        account = [NSKeyedUnarchiver unarchiveObjectWithFile:ZQAccountFilePath];
    });
    //    读取账号 解挡
    //        YCLAccount * account = [NSKeyedUnarchiver unarchiveObjectWithFile:ZQAccountFilePath];
    //    判断账号是否过期
    //        NSDate * now = [NSDate date];
    //        if ([now compare:account.remind_in] != NSOrderedAscending   ) {
    //            account = nil;
    //        }
    return account;
}


+ (void)delAccount {
    TTDPRINT(@"");
    account=nil;
    NSFileManager * defaultManager = [NSFileManager defaultManager];
    if ([defaultManager isDeletableFileAtPath:ZQAccountFilePath]) {
        [defaultManager removeItemAtPath:ZQAccountFilePath error:nil];
    }
}

@end
